package com.yix.admin.modules.system.service.impl;

import com.yix.admin.modules.system.entity.SysDict;
import com.yix.admin.modules.system.entity.SysDictDetail;
import com.yix.admin.modules.system.entity.dto.SysDictDetailDto;
import com.yix.admin.modules.system.entity.mapper.SysDictDetailMapper;
import com.yix.admin.modules.system.entity.query.SysDictDetailQuery;
import com.yix.admin.modules.system.repository.SysDictDetailRepository;
import com.yix.admin.modules.system.repository.SysDictRepository;
import com.yix.admin.modules.system.service.SysDictDetailService;
import com.yix.common.utils.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/2/5 14:24
 */
@Service
@CacheConfig(cacheNames = "dict")
public class SysDictDetailServiceImpl implements SysDictDetailService {

    @Autowired
    private SysDictDetailRepository sysDictDetailRepository;
    @Autowired
    private SysDictRepository sysDictRepository;
    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(SysDictDetailQuery criteria, Pageable pageable) {
        Page<SysDictDetail> page = sysDictDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sysDictDetailMapper::toDto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysDictDetail resources) {
        resources.setCreaterId(SecurityUtils.getCurrentUserId());
        sysDictDetailRepository.save(resources);
        // 清理缓存
        delCaches(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDetail resources) {
        SysDictDetail dictDetail = sysDictDetailRepository.findById(resources.getId()).orElseGet(SysDictDetail::new);
        ValidationUtil.isNull( dictDetail.getId(),"DictDetail","id",resources.getId());
        resources.setId(dictDetail.getId());
        resources.setUpdaterId(SecurityUtils.getCurrentUserId());
        sysDictDetailRepository.save(resources);
        // 清理缓存
        delCaches(resources);
    }

    @Override
    @Cacheable(key = "'name:' + #p0")
    public List<SysDictDetailDto> getDictByName(String name) {
        return sysDictDetailMapper.toDto(sysDictDetailRepository.findByDictName(name));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        SysDictDetail dictDetail = sysDictDetailRepository.findById(id).orElseGet(SysDictDetail::new);
        // 清理缓存
        delCaches(dictDetail);
        sysDictDetailRepository.deleteById(id);
    }

    public void delCaches(SysDictDetail dictDetail){
        SysDict dict = sysDictRepository.findById(dictDetail.getDict().getId()).orElseGet(SysDict::new);
        redisUtils.del(CacheKey.DICT_NAME + dict.getName());
    }
}
